<?	/*
	// File:	parser.inc.php
	// Purpose:	Skin Parser Module
	// Creation:	2001-10-29
	// Author:	Felix <webmaster@can-host.com>
	*/
//

// Ensure we are logged in
if ($webcp_tag && empty($data['failed'])) {
// Check if needed vars are there
if (!isset($cfg))
	return 'parser.inc.phps :: $cfg not loaded (/web/config.inc.php)';
if (!isset($T))
	return 'parser.inc.phps :: $T not loaded (/web/lang/*.phps)';
if (!isset($url))
	return 'parser.inc.phps :: $url not loaded (HTTP GET or /web/init.inc.phps)';
if (!isset($cp))
	return 'parser.inc.phps :: $cp not loaded (HTTP GET or /web/init.inc.phps)';
}

global $menu;
$menu = array();
load_cached_file('menu.default.phps', 'menu');

$menudata = array('userdata' => $userdata, 'personaldata' => $personaldata, 'domaindata' => $domaindata, 'resellerdata' => $resellerdata, 'lang' => $lang);
call_hook('web:menu', $menudata);

if (!isset($skin)) {
	$skin = $cfg['defaultskin'];
}
$tmp = 'parser.class.phps';
include_once($tmp);
//
// Load skin or framed skin
$skinpath = "skin/".$skin;
if (empty($framed)) {
	$skinfile = "skin.tpl";
	if(!file_exists("$skinpath/$skinfile"))
		$skinpath = "skin/default";
} else {
	$skinfile = $framed.'.tpl';
	if(file_exists("$skinpath/$skinfile")) {
		// Check for the needed $framename var
		/*if (empty($framename))
			return 'Target Frame Name is not set';*/
	} elseif (file_exists("$skinpath/skin.tpl"))
		return $T['err']['parser']['framed'];
	else
		return 'Invalid Skin ($skin or $framed)';
}
$parser = new Parser($skinfile, $skinpath);

// Ensure we are logged in
if ($webcp_tag && empty($data['failed'])) {
//
// Prepare value for tag {WHERE AM I} depending on the current Panel viewed
// Prepare value for tag {RESELLER NAME} depending on the current Panel viewed
switch ($cp) {
	case "personal":
		$whereami = $personaldata['username']."@".$domaindata['domain'];
		$resellername = " ".$resellerdata['name'];
		$SQL = 'SELECT name, url, submitform, usernamefield, passwordfield, extra FROM links WHERE personal = 1';
		$personaldata['ownerid'] = $personaldata['id'];
		$ext = mysql_query($SQL);
		break;
	case "domain":
		$whereami = $domaindata['host'].".".$domaindata['domain'];
		$resellername = " ".$resellerdata['name'];
		$SQL = 'SELECT name, url, submitform, usernamefield, passwordfield, extra FROM links WHERE domain = 1';
		$personaldata['ownerid'] = substr($domaindata['id'], 0, 5);
		$ext = mysql_query($SQL);
		break;
	case "reseller":
		$whereami = $resellerdata['name'];
		$resellername = " ".$resellerdata['name'];
		$SQL = 'SELECT name, url, submitform, usernamefield, passwordfield, extra FROM links WHERE reseller = 1';
		$personaldata['ownerid'] = '00000';
		$ext = mysql_query($SQL);
		break;
	case "server":
		$whereami = $cfg['sysname'];
		$resellername = " ";
		$SQL = 'SELECT name, url, submitform, usernamefield, passwordfield, extra FROM links WHERE server = 1';
		$personaldata['ownerid'] = '';
		$ext = mysql_query($SQL);
		break;
	default:
		$whereami = "";
		$resellername = " ";
		break;
}
}

//
// Set simple skin {TAGS} associations
unset($parse);
$parse['WEBCP VERSION'] 	= $cfg['webcp'];
$parse['WEBCP NAME'] 		= "web-cp";
$parse['WEB HOSTING CP'] 	= $T['Web Hosting Control Panel'];
$parse['LICENSE'] 		= $T['web.cp license'];
$parse['NOW'] 			= date($T['date_time_format']);
$parse['LANG']			= $lang;
// Ensure we are logged in
if ($webcp_tag && empty($data['failed'])) {
	$parse['WHERE AM I'] 		= "Managing ".$whereami." -  ".$T[$cp][$url]."";
	$parse['BOOKMARK NAME'] 	= $T['Bookmark'];
	$parse['LOG OUT NAME'] 		= $T['Log Out'];
	$parse['EXTERNAL NAME'] 	= $T['External Tools'];
	$parse['MENU NAME'] 		= $T['menu'][$cp];
	$parse['RESELLER NAME'] 	= $resellername;
	$parse['LOG OUT']			= './?data[failed]=logout';
} else {
	$parse['WHERE AM I'] 		= '';
	$parse['BOOKMARK NAME'] 	= '';
	$parse['LOG OUT NAME'] 		= '';
	$parse['EXTERNAL NAME'] 	= '';
	$parse['MENU NAME'] 		= '';
	$parse['RESELLER NAME'] 	= '';
	$parse['LOG OUT']			= '';
}
$parse['IMG PATH'] 		= "skin/".$skin."/";
$parse['SKIN ID']		= $skin;
$parse['HEADER'] 		= "\n<link rel='stylesheet' type='text/css' href='skin/".$skin."/style.css'>\n<script language='JavaScript' src='script.js'></script>\n";
$parse['SYSNAME'] 		= $cfg['sysname'];
$parse['PORT'] 			= ":".$cfg['port'];;
$parse['P1 NAME'] 		= $T['panel'][1];
$parse['P2 NAME'] 		= $T['panel'][2];
$parse['P3 NAME'] 		= $T['panel'][3];
$parse['P4 NAME'] 		= $T['panel'][4];
$parse['MENU ID'] 		= $cp;
$parse['CHARSET']		= $T['_charset'];
if (empty($framed)) {
	$parse['URL'] = "./?url=".$url."&cp=".$cp."&number=".$number."&user=".$user;
	$parse['BOOKMARK'] = "./?url=".$url."&cp=".$cp."&number=".$number."&user=".$user."&bookmark=true";
	$parse['TARGET ATTR'] = '';
} else {
	$parse['URL'] = "./?url=".$url."&cp=".$cp."&number=".$number."&user=".$user.'framed={TARGET}';
	$parse['TARGET'] = '_top'/*$framename*/;
	$parse['TARGET ATTR'] = ' target="{TARGET}"'; //overridable via template
	$parse['FRAME URL'] = "./?skin=$skin&cp=$cp&url=$url&number=$number&user=$user";
	$parse['BOOKMARK'] = "./?framed={TARGET}&skin=$skin&url=$url&cp=$cp&number=$number&user=$user&bookmark=true";

}
$parser->AddArray($parse);
//MENU ITEMS
$parser->AddClass('MENU');
$parser->AddClass('ALL MENU');
// Ensure we are logged in
if ($webcp_tag && empty($data['failed'])) {

	foreach($menu[$cp] as $menu_url) {
		$menu_name = $T['menu'][$menu_url];
	//do {
		if(empty($framed))
			$parser->AddVar('URL', "./?url=".$menu_url."&cp=".$cp."&number=".$number."&user=".$user, 'MENU');
		else
			$parser->AddVar('URL', "./?framed={TARGET}&url=".$menu_url."&cp=$cp&number=$number&user=$user", 'MENU');
		$parser->AddVar('NAME', $menu_name, 'MENU');
		// Find icon file
		if (file_exists("skin/".$skin."/img/icons/".$menu_url.".gif"))
			$tmpicon = "/skin/".$skin."/img/icons/".$menu_url.".gif";
		elseif (file_exists("skin/".$skin."/img/icons/".$menu_url.".png"))
			$tmpicon = "/skin/".$skin."/img/icons/".$menu_url.".png";
		elseif (file_exists("skin/".$skin."/img/icons/".$menu_url.".jpg"))
			$tmpicon = "/skin/".$skin."/img/icons/".$menu_url.".jpg";
		elseif (file_exists("icon/".$menu_url.".gif"))
			$tmpicon = "icon/".$menu_url.".gif";
		elseif (file_exists("icon/".$menu_url.".png"))
			$tmpicon = "icon/".$menu_url.".png";
		elseif (file_exists("icon/".$menu_url.".jpg"))
			$tmpicon = "icon/".$menu_url.".jpg";
		else
			$tmpicon = "";

		$parser->AddVar('ICON', $tmpicon, 'MENU');

		if($menu_url==$url)
			$parser->AddIf('ACTIVE', true, 'MENU');
		else
			$parser->AddIf('ACTIVE', false, 'MENU');

		$parser->NextClass('MENU');
	//} while (next($T[$cp]));
	}

	foreach($menu as $menu_id => $cur_menu) {
		$parser->AddVar('MENU ID', $menu_id, 'ALL MENU');
		$parser->AddVar('MENU NAME', $T['menu'][$menu_id], 'ALL MENU');

		$parser->AddIf('ACTIVE', $cp==$menu_index, 'ALL MENU');

		$parser->AddClass('MENU', 'ALL MENU');

		foreach($cur_menu as $menu_url) {
			$menu_name = $T['menu'][$menu_url];
			if(empty($framed))
				$parser->AddVar('URL', "./?url=".$menu_url."&cp=".$menu_id."&number=".$number."&user=".$user, 'ALL MENU::MENU');
			else
				$parser->AddVar('URL', "./?framed={TARGET}&url=".$menu_url."&cp=$menu_id&number=$number&user=$user", 'ALL MENU::MENU');
			$parser->AddVar('NAME', $menu_name, 'ALL MENU::MENU');
			// Find icon file
			if (file_exists("skin/".$skin."/img/icons/".$menu_url.".gif"))
				$tmpicon = "/skin/".$skin."/img/icons/".$menu_url.".gif";
			elseif (file_exists("skin/".$skin."/img/icons/".$menu_url.".png"))
				$tmpicon = "/skin/".$skin."/img/icons/".$menu_url.".png";
			elseif (file_exists("skin/".$skin."/img/icons/".$menu_url.".jpg"))
				$tmpicon = "/skin/".$skin."/img/icons/".$menu_url.".jpg";
			elseif (file_exists("icon/".$menu_url.".gif"))
				$tmpicon = "icon/".$menu_url.".gif";
			elseif (file_exists("icon/".$menu_url.".png"))
				$tmpicon = "icon/".$menu_url.".png";
			elseif (file_exists("icon/".$menu_url.".jpg"))
				$tmpicon = "icon/".$menu_url.".jpg";
			else
				$tmpicon = "";

			$parser->AddVar('ICON', $tmpicon, 'ALL MENU::MENU');

			if($menu_url==$url)
				$parser->AddIf('ACTIVE', true, 'ALL MENU::MENU');
			else
				$parser->AddIf('ACTIVE', false, 'ALL MENU::MENU');

			$parser->NextClass('MENU', 'ALL MENU');
		}
		$parser->StripClass('MENU', 'ALL MENU');
		$parser->NextClass('ALL MENU');
	}
}
$parser->StripClass('MENU');
$parser->StripClass('ALL MENU');
//EXTERNAL MENU
$parser->AddClass('EXTERNAL');
$j = 0;

// Ensure we are logged in
if ($webcp_tag && empty($data['failed'])) {
// Obtain real username
$uniquedata['username'] = $personaldata['username'];
$uniquedata['id'] = $personaldata['id'];
call_hook('user:get:username', $uniquedata);
$unique = $uniquedata['username'];

$pd = fetchdata('password','user',Array($user, $number));

if ($ext) {
	for($i=0; $row=mysql_fetch_array($ext); $i++) {
		if ($row['submitform']) {
			$form = '<form target=_new name="externallink'.$i.'" action="'.$row['url'].'" method=post><input type=hidden name="'.$row['usernamefield'].'" value="'.$unique.'"><input type=hidden name="'.$row['passwordfield'].'" value="'.$pd['password'].'">'.$row['extra'];
			$exturl = 'javascript:document.externallink'.$i.'.submit();';
			$formend = '</form>';
			$aproperties = '';
		} else {
			$form = '';
			$exturl = $row['url'];
			$formend = '';
			$aproperties = 'target=_new';
		}
		$parser->AddVar('URL', $exturl, 'EXTERNAL');
		$parser->AddVar('FORMOPEN', $form, 'EXTERNAL');
		$parser->AddVar('FORMEND', $formend, 'EXTERNAL');
		$parser->AddVar('NAME', $row['name'], 'EXTERNAL');
		$parser->AddVar('APROPS', $aproperties, 'EXTERNAL');
		/*
		$tmpurl = $T['external'][$j]['name'];
		// Find icon file
		if (file_exists("skin/".$skin."/img/icons/".$tmpurl.".gif"))
			$tmpicon = "/skin/".$skin."/img/icons/".$tmpurl.".gif";
		elseif (file_exists("skin/".$skin."/img/icons/".$tmpurl.".png"))
			$tmpicon = "/skin/".$skin."/img/icons/".$tmpurl.".png";
		elseif (file_exists("skin/".$skin."/img/icons/".$tmpurl.".jpg"))
			$tmpicon = "/skin/".$skin."/img/icons/".$tmpurl.".jpg";
		elseif (file_exists("icon/".$tmpurl.".png"))
			$tmpicon = "icon/".$tmpurl.".png";
		elseif (file_exists("icon/".$tmpurl.".gif"))
			$tmpicon = "icon/".$tmpurl.".gif";
		elseif (file_exists("icon/".$tmpurl.".jpg"))
			$tmpicon = "icon/".$tmpurl.".jpg";
		else
			$tmpicon = "";
		$parser->AddVar('ICON', $tmpicon, 'EXTERNAL');
		*/
		$parser->NextClass('EXTERNAL');
	}
}
}

$parser->StripClass('EXTERNAL');

if(empty($framed))
	$frame_url = '';
else
	$frame_url = '&framed={TARGET}';

$parser->AddClass('P0');
$parser->StripClass('P0');
$parser->AddClass('P1');
if(is_numeric($userdata['level']) && $userdata['level'] < 2)
	$parser->AddVar('URL', "./?cp=server&number=$number$frame_url", 'P1');
else
	$parser->StripClass('P1');

$parser->AddClass('P2');
if(is_numeric($userdata['level']) && $userdata['level'] < 3)
	$parser->AddVar('URL', "./?cp=reseller&number=$number$frame_url", 'P2');
else
	$parser->StripClass('P2');

$parser->AddClass('P3');
if(is_numeric($userdata['level']) && $userdata['level'] < 4)
	$parser->AddVar('URL', "./?cp=domain&number=$number$frame_url", 'P3');
else
	$parser->StripClass('P3');

$parser->AddClass('P4');
if(is_numeric($userdata['level']) && $userdata['level'] < 5)
	$parser->AddVar('URL', "./?cp=personal&number=$number$frame_url", 'P4');
else
	$parser->StripClass('P4');

$hookdata = Array($parser, $personaldata);
call_hook('web:parser', $hookdata);

//$is_login = $webcp_tag && empty($data['failed']);
$is_login = $webcp_tag && empty($data['failed']);
$parser->AddIf('LOGIN', !$is_login);
$parser->AddIf('NOTLOGIN', $is_login);

$newskin = $parser->parse();

/*$parser->AddClass('NOTLOGIN');
$parser->AddClass('LOGIN');

// Ensure we are logged in
if ($webcp_tag && empty($data['failed'])) {
	$parser->StripClass('LOGIN');
} else {
	$parser->StripClass('NOTLOGIN');
}

$newskin = $parser->parsecode($newskin);*/

?>
